package cn.john;

import cn.john.bean.Person;
import com.google.gson.Gson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class GsonApp {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(GsonApp.class, args);
        Gson gson = context.getBean("gson",Gson.class);
        // 将 Java 对象序列化为 JSON 字符串
        Person person = new Person("John", 30);
        String json = gson.toJson(person);
        System.out.println("Serialized JSON: " + json);

    }
    @Bean
    public Gson gson(){
        System.out.println("从容器中生成Gson对象。。。");
        return new Gson();
    }
}
